//
//  WaypointsCell.m
//  HappyTrip
//
//  Created by dlios on 15-3-26.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "WaypointsCell.h"
#import "UIImageView+WebCache.h"

@implementation WaypointsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.view = [[UIView alloc] init];
        [self.contentView addSubview:self.view];
        self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [_view release];
        
        self.imagView = [[UIImageView alloc] init];
        [self.view addSubview:self.imagView];
        [_imagView release];
        
        self.texLabel = [[UILabel alloc] init];
        self.texLabel.font = [UIFont systemFontOfSize:14];
        [self.view addSubview:self.texLabel];
        [_texLabel release];
        
        self.timeLabel = [[UILabel alloc] init];
        [self.view addSubview:self.timeLabel];
        [_timeLabel release];
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.view.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
    
    CGFloat width = self.view.frame.size.width;

    // 计算图片高度
    NSDictionary *photoDic = [self.waypointsDic objectForKey:@"photo_info"];
    NSString *str = [self.waypointsDic objectForKey:@"photo"];
    if (0 != str.length) {
        // 设置图片frame并请求图片
        if ([self.waypointsDic objectForKey:@"photo_info"] == [NSNull null]) {
            self.imagView.frame = CGRectMake(10, 5, width - 20, self.view.frame.size.width / 2 - 5);
        }else{
            CGFloat h = [[photoDic objectForKey:@"h"] floatValue];
            CGFloat w = [[photoDic objectForKey:@"w"] floatValue];
            CGFloat photoHeigth = self.contentView.frame.size.width * h / w;
            self.imagView.frame = CGRectMake(10, 5, width - 20, photoHeigth - 5);
        }
        NSURL *url = [NSURL URLWithString:str];
        [self.imagView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    }else{
        self.imagView.frame = CGRectMake(10, 0, width - 20, 0);
    }
    
    // 计算文字高度
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    CGRect rect = [self.texLabel.text boundingRectWithSize:CGSizeMake(width, self.contentView.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    // 设置label的frame 并赋值
    self.texLabel.frame = CGRectMake(10, self.imagView.frame.size.height + self.imagView.frame.origin.y + 5, width - 20, rect.size.height);
    self.texLabel.text = [self.waypointsDic objectForKey:@"text"];
    self.texLabel.numberOfLines = 0;
    [self.texLabel sizeToFit];
    
    
    NSString *time = [[self.waypointsDic objectForKey:@"local_time"] substringWithRange:NSMakeRange(5, 5)];
    NSString *month = [[self.waypointsDic objectForKey:@"local_time"] substringWithRange:NSMakeRange(11, 5)];
    self.timeLabel.frame = CGRectMake(10, self.contentView.frame.size.height - 15, self.contentView.frame.size.width - 20, 10);
    self.timeLabel.text = [NSString stringWithFormat:@"⌚️%@ %@  %@", time, month, [self.waypointsDic objectForKey:@"city"]];
    self.timeLabel.font = [UIFont systemFontOfSize:10];
    
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
